package com.wqr.mobile.android.ui.listview;

import java.util.List;

import android.content.Context;
import android.os.AsyncTask.Status;
import android.os.Handler;
import android.os.Handler.Callback;
import android.os.Message;
import android.view.View;
import android.widget.AdapterView;
import android.widget.ImageView;
import android.widget.Toast;

import com.google.gson.Gson;
import com.wqr.mobile.android.ui.BaseListActivity;
import com.wqr.mobile.android.ui.R;
import com.wqr.mobile.android.ui.adapter.MBaseListAdapter;
import com.wqr.mobile.android.ui.api.API;
import com.wqr.mobile.android.ui.bean.ImageData;
import com.wqr.mobile.android.ui.bean.ImageData.ImageItem;
import com.wqr.mobile.android.ui.bean.ImageHolder;
import com.wqr.mobile.android.ui.net.ImageDownloader;
import com.wqr.mobile.android.ui.net.NetworkManager;
import com.wqr.mobile.android.ui.task.HttpRequestTask;
import com.wqr.mobile.android.ui.task.HttpRequestTask.HttpTaskCallBack;
import com.wqr.mobile.android.ui.task.HttpTaskResult;

public class SingleItemImageList extends BaseListActivity implements API, Callback, HttpTaskCallBack {

	private static final int SHOW_LIST = 0;
	private Context mContext;
	private MBaseListAdapter<ImageItem, ImageHolder> mAdapter;

	private List<ImageItem> images;
	private HttpRequestTask mImageTask;
	private NetworkManager mNetworkManager;
	private ImageDownloader mImagedownloader;
	private Handler mHandler;

	// http://api.t.sina.com.cn/search.json?results_per_page=20&page=20&max_id=1344079110&q=%E6%9D%A8%E5%B9%82&source=4008859968

	@Override
	protected void loadData() {
		mContext = this;
		mHandler = new Handler(this);
		mImageTask = new HttpRequestTask(this);
		mImageTask.execute(IMAGE_HOST);
	}

	@Override
	protected void destory() {
		if (mImageTask != null) {
			mImageTask = null;
		}
	}

	@Override
	public void onTaskStart() {
		mHandler.sendEmptyMessage(START);
	}

	@Override
	public HttpTaskResult onTaskRunning(Object... params) {
		String message = (String) params[0];
		if (mNetworkManager == null) {
			mNetworkManager = NetworkManager.getInstance();
		}
		return new HttpTaskResult(message, mNetworkManager.getRequest(mContext, "", message));
	}

	@Override
	public void onCancelled(Status status) {
		// TODO Auto-generated method stub

	}

	@Override
	public void onTaskFinished(HttpTaskResult result) {
		if (result != null && result.getResult() != null) {
			if (result.getMessage().equals(IMAGE_HOST)) {
				Gson g = new Gson();
				ImageData data = g.fromJson(result.getResult(), ImageData.class);
				images = data.images;
				createAdapter();
			}
		}
	}

	private void createAdapter() {
		if (mImagedownloader == null) {
			mImagedownloader = new ImageDownloader(mContext);
		}
		mAdapter = new MBaseListAdapter<ImageItem, ImageHolder>(mContext, R.layout.layout_image_item, new ImageHolder(), images) {

			@Override
			public void findHolderViews(View convertView, ImageHolder holder) {
				holder.icon = (ImageView) convertView;
			}

			@Override
			public void setItemFromHolder(ImageHolder holder, ImageItem item) {
				mImagedownloader.download("http://192.168.0.100:8000/image/" + item.url, holder.icon);
			}

			@Override
			public void _onItemClick(Context context, AdapterView<?> parent, View view, int position, long id, ImageItem item) {
				Toast.makeText(mContext, item.name, 1).show();

			}
		};
		mHandler.sendEmptyMessage(SHOW_LIST);
	}

	@Override
	public boolean handleMessage(Message msg) {
		switch (msg.what) {
		case START:
			this.showLoadingProgressDialog();
			break;
		case FINISHED:
			this.dismissProgressDialog();
			break;
		case SHOW_LIST:
			this.dismissProgressDialog();
			setListAdapter(mAdapter);
			mAdapter.setOnItemClickListener(getListView());
			// setListAdapter(new ArrayAdapter<String>(mContext, android.R.layout.simple_list_item_1, mData));
			break;

		default:
			break;
		}
		return false;
	}

	@Override
	public void onLowMemory() {
		mImagedownloader.clearCache();
		super.onLowMemory();
	}

}
